package cn.qyd.atomic;

import sun.misc.Unsafe;

import java.util.concurrent.TimeUnit;

/**
 * @author 邱运铎
 * @date 2024-03-25 8:48
 */
public class UnsafePark {
    private static Unsafe unsafe;
    static {
        try {
            unsafe = UnsafeDemo01.getUnsafe();
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Thread mainThread = Thread.currentThread();
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(" subThread try to unPark mainThread");
            unsafe.unpark(mainThread);
        }).start();
        System.out.println(" park main thread ");
        unsafe.park(false,0L);
        System.out.println(" success unPark main thread ");
        /**
        //已废弃的， 获取对象锁
        unsafe.monitorEnter(mainThread);
        //已废弃的，释放对象锁
        unsafe.monitorExit(mainThread);
        //已废弃的，尝试获取锁
        unsafe.tryMonitorEnter(mainThread)
         */
    }
}
